package com.admin.infrastructure.persistence.jdbc;

import com.admin.domain.modle.Play;
import com.admin.domain.repository.PlayRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * description User: WeiDa Date: 2017-4-13 Time: 20:38
 */
@Repository
public class PlayRepositoryJdbc implements PlayRepository {

    @Autowired
    protected JdbcTemplate jdbcTemplate;

    @Override
    public int add(Play play) {
        return jdbcTemplate
                .update("INSERT INTO play (play_id,id,`name`,`code`,content,url,minPrice,maxPrice,status,playDate) VALUES (?,?,?,?,?,?,?,?,?,?)",
                        play.getPlayId(), play.getId(), play.getName(),
                        play.getCode(), play.getContent(), play.getUrl(),
                        play.getMinPrice(), play.getMaxPrice(),
                        play.getStatus(), play.getPlayDate());
    }

    @Override
    public int update(Play play) {
        return jdbcTemplate
                .update("UPDATE play SET `name`=?,`saleDate`=?,content=?,url=?,order_num=? WHERE play_id=?",
                        play.getName(), play.getSaleDate(), play.getContent(),
                        play.getUrl(), play.getOrderNum(),play.getPlayId());
    }

    @Override
    public int upsert(List<Play> plays) {
        StringBuilder sql = new StringBuilder();
        sql.append("insert into play (play_id,id,`name`,`code`,content,url,minPrice,maxPrice,status,playDate,hour,minute,saleDate) values");
        Play play = plays.get(0);
        sql.append("(?,?,?,?,?,?,?,?,?,?,?,?,?)");
        Object[] params = new Object[13 * plays.size()];
        params[0] = play.getPlayId();
        params[1] = play.getId();
        params[2] = play.getName();
        params[3] = play.getCode();
        params[4] = play.getContent();
        params[5] = play.getUrl();
        params[6] = play.getMinPrice();
        params[7] = play.getMaxPrice();
        params[8] = play.getStatus();
        params[9] = play.getPlayDate();
        params[10] = play.getHour();
        params[11] = play.getMinute();
        params[12] = play.getSaleDate();
        for (int i = 1; i < plays.size(); i++) {
            play = plays.get(i);
            sql.append(",(?,?,?,?,?,?,?,?,?,?,?,?,?)");
            System.arraycopy(
                    new Object[]{play.getPlayId(), play.getId(),
                            play.getName(), play.getCode(), play.getContent(),
                            play.getUrl(), play.getMinPrice(),
                            play.getMaxPrice(), play.getStatus(),
                            play.getPlayDate(), play.getHour(),
                            play.getMinute(), play.getSaleDate()}, 0, params,
                    i * 13, 13);
        }
        sql.append(" ON DUPLICATE KEY UPDATE ");
        sql.append("code=ifnull(values(code),code),");
        sql.append("content=ifnull(values(content),content),");
        sql.append("url=ifnull(values(url),url),");
        sql.append("minPrice=ifnull(values(minPrice),minPrice),");
        sql.append("maxPrice=ifnull(values(maxPrice),maxPrice),");
        sql.append("status=ifnull(values(status),status),");
        sql.append("playDate=ifnull(values(playDate),playDate),");
        sql.append("hour=ifnull(values(hour),hour),");
        sql.append("minute=ifnull(values(minute),minute),");
        sql.append("saleDate=ifnull(values(saleDate),saleDate)");
        return jdbcTemplate.update(sql.toString(), params);
    }

    @Override
    public Play get(int id) {
        return jdbcTemplate.queryForObject(
                "select * from play where id=?",
                BeanPropertyRowMapper.newInstance(Play.class), id);
    }

    @Override
    public Play get(String id) {
        return jdbcTemplate.queryForObject(
                "select * from play where play_id=?",
                BeanPropertyRowMapper.newInstance(Play.class), id);
    }

    @Override
    public List<Play> list(String key) {
        if (StringUtils.isEmpty(key)) {
            return jdbcTemplate.query("select * from play order by order_num desc",
                    BeanPropertyRowMapper.newInstance(Play.class));
        } else {
            return jdbcTemplate.query("select * from play WHERE `name` like ? order by order_num desc",
                    BeanPropertyRowMapper.newInstance(Play.class), "%" + key + "%");
        }

    }

    @Override
    public void remove(String id) {
        jdbcTemplate.update("DELETE FROM play WHERE play_id=?", id);
    }

    @Override
    public void updateSaleDateById(String playId, String saleDate) {
        jdbcTemplate.update("UPDATE play SET `saleDate`=? WHERE play_id=?",saleDate,playId);
    }
}
